package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;

@Repository("ExpireDAO")
@Transactional
public class ExpireDAOImpl extends AbstractJpaDao<Expire> implements ExpireDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Expire.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public ExpireDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Expire findExpireById(Integer id) throws DataAccessException {

		return findExpireById(id, -1, -1);
	}

	@Transactional
	public Expire findExpireById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findExpireById", startResult, maxRows, id);
			return (Expire) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Expire> findExpireByName(String name) throws DataAccessException {

		return findExpireByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findExpireByName", startResult, maxRows, name);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@Transactional
	public Set<Expire> findExpireByNameContaining(String name) throws DataAccessException {

		return findExpireByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findExpireByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@Transactional
	public Set<Expire> findExpireByQuantity(BigDecimal quantity) throws DataAccessException {

		return findExpireByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findExpireByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@Transactional
	public Set<Expire> findExpireByDescription(String description) throws DataAccessException {

		return findExpireByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findExpireByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@Transactional
	public Set<Expire> findExpireByDescriptionContaining(String description) throws DataAccessException {

		return findExpireByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findExpireByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@Transactional
	public Expire findExpireByPrimaryKey(Integer id) throws DataAccessException {
		return findExpireByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Expire findExpireByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findExpireByPrimaryKey", startResult, maxRows, id);
			return (Expire) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Expire> findExpiresByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findExpiresByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Expire> findAllExpires() throws DataAccessException {

		return findAllExpires(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findAllExpires(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllExpires", startResult, maxRows);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Expire> findExpiresByUnit(Integer unitId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllExpiresByUnit", start, max, unitId);
		return new LinkedHashSet<Expire>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Expire> findExpireByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Expire>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Expire> findExpiresByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Expire.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Expire> expires = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return expires;
	}

	@Transactional
	public Expire findExpireByPhysicalPrimaryKey(BigDecimal quantity, Integer unitId) throws DataAccessException {
		return findExpireByPhysicalPrimaryKey(quantity, unitId, -1, -1);
	}

	@Transactional
	public Expire findExpireByPhysicalPrimaryKey(BigDecimal quantity, Integer unitId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findExpireByPhyscialPrimaryKey", startResult, maxRows, quantity, unitId);
			return (Expire) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Expire entity) {
		return true;
	}
}
